<?php

namespace App\Model;

use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Model\Merchants\MerchantsModel;
use App\Model\Warehouse\WarehouseModel;
use App\Util\UserIdentityHelper;

/**
 * Class UserModel
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $salt
 * @property string $email
 * @property string $name
 * @property string $phone
 */
class UserModel extends BaseModel
{
    
    protected $tableName = 'ss_user';
    protected static $logTableName = 'ss_user_log';
    
    protected $casts = [
        'id'         => 'integer',
        'role_id'    => 'integer',
        'username'   => 'string',
        'salt'       => 'string',
        'password'   => 'string',
        'email'      => 'string',
        'name'       => 'string',
        'phone'      => 'string',
        'vip_state'  => 'integer',
        'type'       => 'integer',
    ];
    
    const TYPE_INSIDE = 1;//平台账号
    const TYPE_BUSINESS = 2;//推广员账号
    const TYPE_WAREHOUSE = 3;//仓库账号

    const SUPER_ADMIN = 1;//超级管理员
    const STATE_WAIT = 1;//待审核
    const STATE_SUCCESS = 2;//审核成功
    const STATE_FAIL = 3;//审核失败

    public static function getTypeDict(?string $key = null)
    {
        return self::getMap([
            self::TYPE_INSIDE    => '平台账号',
            self::TYPE_BUSINESS  => '推广员账号',
            self::TYPE_WAREHOUSE => '仓库账号',
        ], $key);
    }
    
    public static function getIsParentDict(?string $key = null)
    {
        return self::getMap([
            self::DISABLED => '子',
            self::ENABLED  => '父',
        ], $key);
    }
    
    public static function getStateDict($key = null)
    {
        return self::getMap([
            self::STATE_WAIT    => '待审核',
            self::STATE_SUCCESS => '审核成功',
            self::STATE_FAIL    => '审核失败',
        ], $key);
    }
    
    
    /**
     * 重置密码
     * @param string $user_id 用戶id
     * @param string $password 密码
     * @param string $salt 密码种子
     * @return \App\Core\BaseModel|\EasySwoole\ORM\AbstractModel
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function resetPassword($user_id, $password, $salt)
    {
        $this->connection('write');
        $data = [
            'salt'     => $salt,
            'password' => $password,
        ];
        
        return self::updateData($data, ['id' => $user_id]);
    }

    /**
     * @param int $type 类型
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     * @Time: 2022/4/13 14:44
     */
    public function userList(int $type = 0,bool $is_parent = false){
        $where = [
            'state' => self::ENABLED,
        ];

        if ($type) {
            $where['type'] = $type;
        }
        return self::makeQueryModel($where)->field(['id','username'])->all()->toArray();
    }

    /**
     * @param $id
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function searchUserById($id){
        if (!$id) {
            throw new DefaultException('找不到对应的盘点人员');
        }
        $where = [
            'state' => self::ENABLED,
            'id'         => $id,
        ];
//        $data = self::makeQueryModel($where)->field(['id', 'username'])->get()->toArray();
        return self::getOne($where,true,['id','username']);
    }

    public function searchByMerchantsId($merchants_id){
        if (!$merchants_id) {
            throw new DefaultException('找不到对应的人员信息');
        }
        return UserModel::getOne(['merchants_id'=>$merchants_id],true);
    }
}